package com.anlu.alspringmvc.spring.framework.beans.support;

import com.anlu.alspringmvc.spring.framework.beans.config.ALBeanDefinition;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * bean的读取类
 */
public class ALBeandefinitionReader {

    private List<String> registryBeanClasses = new ArrayList<>();
    private Properties contextConfig = new Properties();


    public ALBeandefinitionReader(String... configLocations) {
        doLoadConfig(configLocations[0]);
        //扫描配置文件中的配置的相关类
        doScanner(contextConfig.getProperty("scanPackage"));

    }

    private ALBeanDefinition doCreateBeanDefenition(String beanName, String beanClassName){
        ALBeanDefinition beanDefinition = new ALBeanDefinition();
        beanDefinition.setFactoryBeanName(beanName);
        beanDefinition.setBeanClassName(beanClassName);
        return beanDefinition;
    }
    private void doScanner(String scanPackage){
        //jar 、 war 、zip 、rar
        URL url = this.getClass().getClassLoader().getResource("/" + scanPackage.replaceAll("\\.","/"));
        File classPath = new File(url.getFile());

        for(File file : classPath.listFiles()){
            if(file.isDirectory()){
                doScanner(scanPackage + "." + file.getName());
            }else{
                if(!file.getName().endsWith(".class")){
                    continue;
                }
                //全类名 = 包名.类名
                String className = (scanPackage + "." + file.getName().replace(".class", ""));
                //Class.forName(className);
                registryBeanClasses.add(className);
            }
        }
    }

    /**
     * 加载bean文件
     * @return
     */
    public List<ALBeanDefinition> loadBeanDefinitions(){
        List<ALBeanDefinition> result = new ArrayList<>();
        try{
            for(String className : registryBeanClasses){
                Class<?> beanClass = Class.forName(className);
                //如果是接口 则不进行加载
                if(beanClass.isInterface()){
                    continue;
                }
                /**
                 * 保存类对应的ClassName
                 * beanName
                 * 默认是类名首字母小写
                 */
                result.add(doCreateBeanDefenition(toLowerFirstCase(beanClass.getSimpleName()),beanClass.getName()));

                //接口注入
                for(Class<?> i : beanClass.getInterfaces()){
                    result.add(doCreateBeanDefenition(i.getName(),beanClass.getName()));
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return result;
    }

    private void doLoadConfig(String contextConfigLocation){
        InputStream is = this.getClass().getClassLoader().getResourceAsStream(contextConfigLocation.replaceAll("classpath:",""));
        try{
            contextConfig.load(is);
        } catch (IOException e){
            e.printStackTrace();
        } finally {
            if(null != is){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //自己写，自己用
    private String toLowerFirstCase(String simpleName) {
        char [] chars = simpleName.toCharArray();
//        if(chars[0] > )
        chars[0] += 32;
        return String.valueOf(chars);
    }

    public Properties getConfig(){
        return this.contextConfig;
    }
}
